#include "index.h"
#include "ui_index.h"
#include <QFontDatabase>
#include <QRect>


Index::Index(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Index)
{
    ui->setupUi(this);
    int fontId = QFontDatabase::addApplicationFont("./content/fonts/fontawesome-webfont.ttf");
    QString fontName = QFontDatabase::applicationFontFamilies(fontId).at(0);    //获取字体名称
    QFont iconFont = QFont(fontName);
    iconFont.setPixelSize(128);

    setWindowIcon(QIcon("./content/imgs/catear.svg"));


    setWindowFlags(Qt::FramelessWindowHint);    //隐藏标题栏（无边框）
    setAttribute(Qt::WA_StyledBackground);      //启用样式背景绘制
    setAttribute(Qt::WA_TranslucentBackground); //背景透明
    setAttribute(Qt::WA_Hover);


    defaultSize = geometry();
    setContentsMargins(11,11,11,11);

    shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);
    shadow_effect->setColor(QColor(97, 97, 97, 155));

    shadow_catear_l = new QGraphicsDropShadowEffect(ui->CatEar_Left);
    shadow_catear_l->setOffset(0, 0);
    shadow_catear_l->setColor(QColor(38, 38, 38, 127));

    shadow_catear_r = new QGraphicsDropShadowEffect(ui->CatEar_Right);
    shadow_catear_r->setOffset(0, 0);
    shadow_catear_r->setColor(QColor(38, 38, 38, 127));

    SetShadow(true);

    ui->settingsNavButton->setFont(iconFont);
    ui->indexNavButton->setFont(iconFont);
    ui->closeButton->setFont(iconFont);
    ui->changeSizeButton->setFont(iconFont);
    ui->minimizeButton->setFont(iconFont);

    ui->indexNavButton->setText(QChar(0xf015));
    ui->settingsNavButton->setText(QChar(0xf085));

    ui->closeButton->setText(QChar(0xf00d));
    ui->changeSizeButton->setText(QChar(0xf2d0));
    ui->minimizeButton->setText(QChar(0xf2d1));

    ResizeCatEar();
}

Index::~Index()
{
    delete ui;
}

void Index::ResizeCatEar(int w, int h){
    ui->CatEar_Left->setMinimumSize(w,h);
    ui->CatEar_Right->setMinimumSize(w,h);
}

void Index::SetShadow(bool shad){
    if(shad){
        shadow_effect->setBlurRadius(11);
        shadow_catear_l->setBlurRadius(11);
        shadow_catear_r->setBlurRadius(11);
        ui->Total->setGraphicsEffect(shadow_effect);

        ui->CatEar_Left->setGraphicsEffect(shadow_catear_l);
        ui->CatEar_Right->setGraphicsEffect(shadow_catear_r);
        ui->CatEarWidget->setContentsMargins(0,11,0,0);
        setContentsMargins(11,11,11,11);
    }
    else {
        shadow_effect->setBlurRadius(0);
        shadow_catear_l->setBlurRadius(0);
        shadow_catear_l->setBlurRadius(0);
        ui->Total->setGraphicsEffect(shadow_effect);
        ui->CatEar_Left->setGraphicsEffect(shadow_catear_l);
        ui->CatEar_Right->setGraphicsEffect(shadow_catear_r);
        ui->CatEarWidget->setContentsMargins(0,0,0,0);
        setContentsMargins(0,0,0,0);
    }
}


void Index::on_closeButton_clicked()
{
    QApplication::exit();
}

void Index::on_changeSizeButton_clicked()
{
    if(windowState() == Qt::WindowMaximized){
        //还原正常大小
        setWindowState(Qt::WindowNoState);
        SetShadow(true);
        ResizeCatEar();
        //设置按钮为最大化按钮
        ui->changeSizeButton->setText(QChar(0xf2d0));
    }
    else if(windowState() == Qt::WindowNoState){
        //最大化
        setWindowState(Qt::WindowMaximized);
        SetShadow(false);
        ResizeCatEar(0,0);
        //设置按钮为还原窗口按钮
        ui->changeSizeButton->setText(QChar(0xf2d2));
    }
}

void Index::on_minimizeButton_clicked()
{
    if(windowState() == Qt::WindowMaximized){
        //最大化时按下按钮
        ui->changeSizeButton->setText(QChar(0xf2d0));
    }
    else if(windowState() == Qt::WindowNoState){
        //正常时按下按钮
    }
    setWindowState(Qt::WindowMinimized);
    SetShadow(true);
    ResizeCatEar();
}


void Index::mousePressEvent(QMouseEvent *event)
{
    if(windowState() == Qt::WindowMaximized){
        //最大化状态不准拖动
        return;
    }
    if( (event->button() == Qt::LeftButton) ){
        mouse_press = true;
        mousePoint = event->globalPos() - this->pos();
    }

}
void Index::mouseMoveEvent(QMouseEvent *event)
{
    if(windowState() == Qt::WindowMaximized){
        //最大化状态不准拖动
        return;
    }
    if(mouse_press){
        move(event->globalPos() - mousePoint);
    }
}

void Index::mouseReleaseEvent(QMouseEvent *event)
{
    mouse_press = false;
}
